<?php
// +----------------------------------------------------------------------
// | sfadmin - Suifeng Administration
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://suifeng.tech All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: suifeng <1787585212@qq.com>
// +----------------------------------------------------------------------

namespace suifeng\helper;

class ArrayHelper
{

    /**
     * 以foo.bar.xyz方式获取数组值。
     *
     * @param unknown $array
     * @param unknown $key
     * @return NULL
     */
    public static function get($array, $key)
    {
        $key_list = explode('.', $key);
        $last_key = array_pop($key_list);

        if ($key_list) {
            $parent =& $array;

            foreach ($key_list as $s) {
                if (isset($parent[$s]) && is_array($parent[$s])) {
                    $parent =& $parent[$s];
                } else {
                    return null;
                }
            }
        } else {
            $parent =& $array;
        }

        if (isset($parent[$last_key])) {
            return $parent[$last_key];
        } else {
            return null;
        }
    }
}
